package com.cr.admin.business.banner.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.cr.admin.business.banner.pojo.entity.Banner;
import com.cr.admin.business.banner.pojo.param.BannerAddParam;
import com.cr.admin.business.banner.pojo.param.BannerUpdateParam;
import com.cr.admin.business.banner.pojo.query.BannerQuery;
import com.cr.admin.business.banner.service.BannerService;
import com.cr.admin.business.topic.pojo.entity.Topic;
import com.cr.admin.business.topic.pojo.param.TopicAddParam;
import com.cr.admin.business.topic.pojo.param.TopicUpdateParam;
import com.cr.admin.business.topic.pojo.query.TopicQuery;
import com.cr.common.response.JsonResult;
import com.cr.common.response.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "横幅管理")
@RequestMapping("/api/bus/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    @SaCheckPermission("bus:content:banner:query")
    @ApiOperation(value = "查询横幅列表")
    @GetMapping
    public JsonResult list(BannerQuery bannerQuery) {
        PageData<Banner> pageData = PageData.page(bannerQuery.getPageNum(), bannerQuery.getPageSize(), () -> bannerService.list(bannerQuery));
        return JsonResult.success(pageData);
    }

    @SaCheckPermission("bus:content:banner:add")
    @ApiOperation(value = "新增横幅")
    @PostMapping
    public JsonResult insert(@Validated @RequestBody BannerAddParam bannerAddParam) {
        bannerService.insert(bannerAddParam);
        return JsonResult.success();
    }

    @SaCheckPermission("bus:content:banner:edit")
    @ApiOperation(value = "修改横幅")
    @PutMapping()
    public JsonResult update(@Validated @RequestBody BannerUpdateParam bannerUpdateParam) {
        bannerService.updateById(bannerUpdateParam);
        return JsonResult.success();
    }

    @SaCheckPermission("bus:content:banner:query")
    @ApiOperation(value = "查询横幅详情")
    @GetMapping("{id}")
    public JsonResult getDetail(@PathVariable("id") Long id) {
        Banner banner = bannerService.selectById(id);
        return JsonResult.success(banner);
    }

    @SaCheckPermission("bus:content:banner:del")
    @ApiOperation(value = "删除横幅")
    @DeleteMapping()
    public JsonResult delete(@RequestParam(name = "ids") List<Long> ids){
        bannerService.deleteByIds(ids);
        return JsonResult.success();
    }

}
